package com.sniff.atp.modules.test.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sniff.atp.common.api.CommonPage;
import com.sniff.atp.common.api.CommonResult;
import com.sniff.atp.modules.test.model.BaseData;
import com.sniff.atp.modules.test.service.BaseDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author macro
 * @since 2024-03-21
 */
@RestController
@RequestMapping("/test/baseData")
@Api(value = "数据字典查询", tags = "数据字典查询")
public class BaseDataController {

    @Autowired
    private BaseDataService baseDataService;

    @ApiOperation("新增")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult create(@RequestBody BaseData baseData) {
        boolean success = baseDataService.create(baseData);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult update(
            @RequestBody BaseData baseData) {

        boolean success = baseDataService.update(baseData.getId(), baseData);
        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<BaseData> getItem(@PathVariable Long id) {
        BaseData baseData = baseDataService.getById(id);
        return CommonResult.success(baseData);
    }

    @ApiOperation("根据ID删除")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@PathVariable Long id) {
        boolean success = baseDataService.removeById(id);

        if (success) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询问题")
    @RequestMapping(value = "/pageList", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<BaseData>> pageList(@RequestBody BaseData baseData) {
        if(baseData.getPageSize() == null){
            baseData.setPageSize(10);
        }
        if(baseData.getPageNum() == null){
            baseData.setPageNum(1);
        }
        Page<BaseData> menuList = baseDataService.pageList(baseData, baseData.getPageSize(), baseData.getPageNum());
        return CommonResult.success(CommonPage.restPage(menuList));
    }

    @ApiOperation("查询list")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<BaseData>> list(@RequestBody BaseData baseData) {

        List<BaseData> menuList = baseDataService.list(baseData);
        return CommonResult.success(menuList);
    }

    @ApiOperation("查询单个数据类型list")
    @RequestMapping(value = "/singlelist", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<String>> singlelist(@RequestBody BaseData baseData) {

        List<String> menuList = baseDataService.singlelist(baseData);
        return CommonResult.success(menuList);
    }
}

